home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / cli / shelltools.lha / time / time.c next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  2.3 KB  |  100 lines

  1. ;/* time - compiles with SAS/C 5.10a by executing this file
  2. lc -cfis -v -b0 -j73 -O -M time
  3. blink time.o to time lib lib:lcnb.lib lib:amiga.lib SC ND VERBOSE
  4. quit ;*/
  5. /*
  6. *    time - measure time command takes to complete.
  7. *
  8. *    Usage: time <command-and-its-args> 
  9. *
  10. *    Martin W. Scott, 4/92.
  11. */
  12. #include <exec/types.h>
  13. #include <dos/dos.h>
  14. #include <dos/rdargs.h>
  15.  
  16. #include <proto/exec.h>        /* use PRAGMAS */
  17. #include <proto/dos.h>
  18.  
  19. void main(void);
  20. void PrintDateStamp(struct DateStamp *);
  21. void SubDateStamp(struct DateStamp *, struct DateStamp *);
  22.  
  23.  
  24. char version_str[] = "$VER: time v1.0";
  25. struct DosLibrary *DOSBase;
  26. struct RDArgs rdargs;
  27.  
  28. void main(void)        /* entry: start command and print how long it took */
  29. {
  30.     struct DateStamp before, after;
  31.     struct RDArgs *readargs;
  32.     LONG rargs[1];
  33.  
  34.     if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L))
  35.     {
  36.         rdargs.RDA_ExtHelp = "Usage: time <command>\n prints time command takes to execute, in form HH:MM:SS.SS\n";
  37.         if (readargs = ReadArgs("COMMAND/A/F", rargs, &rdargs))
  38.         {
  39.             DateStamp(&before);
  40.             System((char *)rargs[0], NULL);
  41.             DateStamp(&after);
  42.             SubDateStamp(&after,&before);
  43.             PrintDateStamp(&after);
  44.         }
  45.         else PrintFault(IoErr(), "time");
  46.  
  47.         CloseLibrary(DOSBase);
  48.     }
  49.  
  50. } /* main */
  51.  
  52. #include <stdarg.h>
  53. LONG __stdargs DosPrintf(char *s, ...);
  54.  
  55. LONG __stdargs DosPrintf(char *s, ...)        /* stub for VPrintf */
  56. {
  57.     va_list ap;
  58.     va_start(ap,s);
  59.     return VPrintf(s, (long *)ap);
  60. }
  61.  
  62.  
  63. #define TICKS_PER_MINUTE    (TICKS_PER_SECOND*60)
  64. #define MINUTES_PER_DAY        (60*24)
  65.  
  66.  
  67. void PrintDateStamp(struct DateStamp *ds)    /* print datestamp as HH:MM:SS.SS */
  68. {
  69.     LONG h,m,s,hs;
  70.  
  71.     h = ds->ds_Days*24 + ds->ds_Minute / 60;        /* hours */
  72.     m = ds->ds_Minute % 60;            /* minutes */
  73.     s = ds->ds_Tick / TICKS_PER_SECOND;        /* seconds */
  74.                             
  75.     hs = (100 *                    /* hundreths of a second */ 
  76.         (ds->ds_Tick % TICKS_PER_SECOND)) / TICKS_PER_SECOND;
  77.  
  78.     DosPrintf("time %ld:%02ld:%02ld.%02ld\n", h,m,s,hs);
  79. }
  80.  
  81. void SubDateStamp(struct DateStamp *ds, struct DateStamp *amount)
  82. {
  83.     /* subtract amount from ds */
  84.  
  85.     if (ds->ds_Tick < amount->ds_Tick)
  86.     {
  87.         ds->ds_Tick += TICKS_PER_MINUTE;
  88.         ds->ds_Minute--;
  89.     }
  90.  
  91.     if (ds->ds_Minute < amount->ds_Minute)
  92.     {
  93.         ds->ds_Minute += MINUTES_PER_DAY;
  94.         ds->ds_Days--;
  95.     }
  96.  
  97.     ds->ds_Days -= amount->ds_Days;
  98.     ds->ds_Minute -= amount->ds_Minute;
  99.     ds->ds_Tick -= amount->ds_Tick;
  100. }